//基础列表页面，不含状态筛选与添加按钮
var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
    behaviors: [basePage, baseList],

    /**
     * 页面的初始数据
     */
    data: {
        checkMobile: false,
        /**是否播放视频/视频地址 */
        showVideo: false,
        reqUrl: '/home/ump/sweep/product',
        /**预支付地址 */
        preUrl: '/home/ump/sweep/preinfo',
        /**购买弹窗 */
        addCart: false,
        /**购买数量 */
        nums: 1,
        /**选中的产品 */
        pdc: {},
        /**分享链接 */
        path: '/pages/cls/list/BaseList',
        /**页面标题 */
        title: '',
        active: 0,
        urlList: ["/home/supplier/product/index", "/home/yuyue/index?from=dounan_yuyue", ""],
        showSubscribeClick: false,
        catIndex: 0, //分类索引
        catData: [],
        showstate: false,
        wh_id: 1,
        showTab:true
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("options", options);
        console.log("wh_id", getApp().globalData.warehouse.wh_id)
        this.data.path = '/pages/cls/list/BaseList?title=扫底专区';
        if (options.title) {
            if(options.title == "捡漏"){
                wx.setNavigationBarTitle({
                    title: "仓库捡漏",
                })
            }else{
                wx.setNavigationBarTitle({
                    title: options.title,
                })
            }
            
            this.setData({
                title: options.title,
            })
            // this.data.title=options.title;
            this.data.path = '/pages/cls/list/BaseList?title=' + options.title;
        }
        let wh_id = getApp().globalData.warehouse.wh_id;
        if (options.wh_id) {
            wh_id = options.wh_id
        }
        this.setData({
            wh_id: wh_id
        })
        if (options.reqUrl && options.title == "斗南专享") {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id + "&is_dounan=1";
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;
            this.init_pdc()
            this.getArticle();
        } else if (options.reqUrl && (options.title == "秒杀专区")) {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id + "&is_seckill=1";
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;

            this.request({
                url: this.data.host + '/home/productCate?wh_id=' + wh_id + "&from=flower&is_seckill=1&live_stream=0"
            }, this.get_cate_ok);

        } else if (options.reqUrl && options.title == "市场捡漏" || options.title == "捡漏") {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id + "&live_stream=1";
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;
            this.getArticle2(wh_id);
            this.setData({
                showstate: true
            })
            this.request({
                url: this.data.host + '/home/productCate?wh_id=' + wh_id + "&from=flower&is_seckill=0&live_stream=1"
            }, this.get_cate_ok);

        } else if (options.reqUrl && options.title == "次日达") {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id + "&is_next_day=1";
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;

            this.request({
                url: this.data.host + '/home/productCate?wh_id=' + wh_id + "&from=flower&is_next_day=1"
            }, this.get_cate_ok);

        } else if (options.reqUrl && options.title == "跨区域") {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id + "&from=regional";
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;

            this.request({
                url: this.data.host + '/home/productCate?wh_id=' + wh_id + "&from=regional"
            }, this.get_cate_ok);

        } else {
            this.data.reqUrl = options.reqUrl + "?wh_id=" + wh_id;
            this.data.path = this.data.path + "&reqUrl=" + options.reqUrl;
            this.init_pdc()
        }
        if (options.preUrl) {
            this.data.preUrl = options.preUrl;
            this.data.path = this.data.path + "&preUrl=" + options.preUrl;
        }
        // options.reqUrl ? this.data.reqUrl = options.reqUrl : '';
        // options.preUrl ? this.data.preUrl = options.preUrl : '';
        // options.title?:'';

        // let catArr = getApp().globalData.cateArr;
        // console.log("catArr", catArr)
        // if (!catArr || !catArr.length) { //没有分类缓存
        //   // 请求分类列表
        //   return this.request({
        //     url: this.data.host + '/home/product/cate?limit=1000'
        //   }, this.get_cate_ok);
        // }else{
        // 	this.setData({
        // 		catData: catArr
        // 	})
        // 	this.init_pdc()
        // }


        // this.req_items();
    },
    init_pdc() {
        // console.log("this.data.catIndex",this.data.catIndex)
        let data = this.data;
        if (this.data.title != "斗南专享") {
            this.data.reqData = {
                cate_id: data.catData[this.data.catIndex].cate_id,
            }
        }

        data.current_page = 0;

        this.req_items();
    },
    get_cate_ok(res) {
        //  console.log("get_cate_ok", res);
        // getApp().globalData.cateArr = res.data;
        this.setData({
            catData: res.data
        })
        if (res.data && res.data.length) {
            this.init_pdc()
        }
        // else {
        //   wx.showToast({
        //     title: '暂无产品',
        //     icon: 'none'
        //   })
        // }
    },
    cat_change(e) {
        // console.log("cat_change", e);
        this.setData({
            catIndex: e.detail
        });

        this.init_pdc();
    },
    getArticle() {
        this.request({
            url: this.data.host + '/home/article/detail?article_id=38'
        }, this.get_detail_ok);
    },
    getArticle2(wh_id) {
        //          宝鸡仓6 85
        //          榆林仓2 86
        //          鄂尔多斯仓10  87
        //          包头仓9 88
        //          呼市仓8  89
        //          延安仓5  90
        //          西安3    98
        // var article_id=""
        // if(wh_id==6){
        //     article_id=85
        // }else if(wh_id==2){
        //     article_id=86
        // }else if(wh_id==10){
        //     article_id=87
        // }else if(wh_id==9){
        //     article_id=88
        // }else if(wh_id==8){
        //     article_id=89
        // }else if(wh_id==5){
        //     article_id=90
        // }else if(wh_id==3){
        //     article_id=98
        // }
        // if(!article_id) return;
        // this.request({
        //     url: this.data.host + '/home/article/detail?article_id='+article_id
        // }, this.get_detail_ok);

        this.request({
            url: this.data.host + '/home/whDeliveryMsg?type=2' + "&wh_id=" + wh_id
        }, res => {
            var article_content = ""
            if (res.data.article) {
                article_content = res.data.article.content.replace(/<img /ig, '<img width="100%" ')
                                    .replace(/<img [^>]*src=['"]/g, `<img src="${this.data.imgHost}/`)
                                    .replace(/<img/gi, '<img class="fwb-img2"')
            }
            this.setData({
                article_content: article_content
            });
        });
    },
    get_detail_ok(res) {
        console.log("get_detail_ok", res);
        var article_content = ""
        if (res.data.article_content.content) {
            article_content = res.data.article_content.content.replace(/<img /ig, '<img width="100%" ')
        }
        this.setData({
            article_content: article_content
        });
    },
    search_click(e) {
        console.log("search_click", e);
        this.data.current_page = 0;
        this.data.reqData.so = e.detail;
        // this.data.reqData = {
        //   so: e.detail
        // }
        this.req_items();
    },
    login_reload() {
        this.req_items();
    },
    nums_change(e) {
        // this.data.nums = e.detail;
        this.setData({
            nums: e.detail
        })
        // console.log("nums_change", e)
    },
    toDetail(e) {
        console.log(e)
        var pdc = e.detail
        var from = "seckill"
        if (this.data.title == "秒杀专区") { //秒杀
            from = "seckill"
        } else if (this.data.title == "斗南专享") { //斗南
            from = "dounan"
        } else if (this.data.title == "市场捡漏" || this.data.title == "捡漏") {
            from = "live_stream"
        } else if (this.data.title == "次日达") { //
            from = "next_day"
        } else if (this.data.title == "跨区域") {
            from = "regional"
        }
        wx.navigateTo({
            url: '/pages/goodsDetail/goodsDetail?pid=' + pdc.pid + "&supplier_id=" + pdc.supplier_id +
                "&from=" + from + "&wh_id=" + this.data.wh_id
        })
    },
    //加入购物车
    add_cart() {
        let pdc = this.data.pdc
        let data = {
            pid: pdc.pid,
            nums: this.data.nums,
        }

        if (this.data.title == "秒杀专区") { //秒杀
            data.from = "seckill"
        } else if (this.data.title == "斗南专享") { //斗南
            data.from = "dounan"
        } else if (this.data.title == "市场捡漏" || this.data.title == "捡漏") {
            data.from = "live_stream"
        } else if (this.data.title == "次日达") { //斗南
            data.from = "next_day"
        } else if (this.data.title == "跨区域") {
            data.from = "regional"
        }

        this.request({
            url: this.data.host + '/home/cart/addToCart',
            data: data,
            method: 'post'
        }, this.add_cart_ok);
    },
    /**
     * 添加购物车结果处理
     * @param {*} res  后台返回的数据
     */
    add_cart_ok(res) {
        // console.log("add_cart_ok",res);
        // this.setData({nums:1})
        this.setData({
            addCart: false
        })
        wx.showToast({
            title: res.msg,
            icon: 'success'
        })
    },
    onCloseCart() {
        this.setData({
            addCart: false
        })
    },
    //立即购买
    buy_good() {
        console.log("立即购买", this.data.pdc)
        let pdc = this.data.pdc
        let data = {
            pid: pdc.pid,
            nums: this.data.nums,
            wh_id: this.data.wh_id
        }
        if (this.data.title == "秒杀专区") { //秒杀
            data.from = "seckill"
        } else if (this.data.title == "斗南专享") { //斗南
            data.from = "dounan"
        } else if (this.data.title == "市场捡漏" || this.data.title == "捡漏") {
            data.from = "live_stream"
        } else if (this.data.title == "次日达") { //次日达
            data.from = "next_day"
        } else if (this.data.title == "跨区域") {
            data.from = "regional"
        } else {
            data.from = "flower"
        }

        this.request({
            url: this.data.host + '/home/flower/preInfo',
            data: data,
            method: 'get'
        }, res => {
            this.setData({
                addCart: false
            })
            res.data.cashier_decca = this.get_thumb(res.cashier_decca, 'wide');
            let that = this;
            wx.navigateTo({
                // url: '/pages/order_confirm/order_confirm',
                url: '/pages/neworder_confirm/order_confirm',
                success: function (obj) {
                    // 通过eventChannel向被打开页面传送数据

                    obj.eventChannel.emit('orderData', {
                        wh_id: that.data.wh_id,
                        // index: that.data.areaData[that.data.tabIndex].index,
                        data: res.data,
                        m_addr: res.m_addr,
                        activeCar: data.from,
                        wuliu: res.data.freight_list,
                        specialList: res.data.extra_list,
                        pack_list: res.data.pack_list,
                        buyType: 1,
                        order_pei_type: res.order_pei_type ? res.order_pei_type : "1",
                        orderWarehouse: res.warehouse
                    })
                }
            })
        });
    },
    /**
     * 下预约单
     */
    // add_cart() {
    //   this.request({
    //     url: this.data.host + this.data.preUrl,
    //     data: {
    //       presell_id:this.data.pdc.presell_id,
    //       sweep_id: this.data.pdc.sweep_id,
    //       seckill_id:this.data.pdc.seckill_id,
    //       nums: this.data.nums,
    //       wh_id:getApp().globalData.warehouse.wh_id
    //     },
    //     method: 'GET'
    //   }, this.add_preinfo_ok);
    // },
    add_preinfo_ok(res) {
        // console.log('add_preinfo_ok', res);
        let noShowNum = this.data.title == "斗南专享" ? '1' : ''; //斗南专享下单时不提示起购数量
        wx.navigateTo({
            url: '/pages/preSaleList/preinfo/Order?noShowNum=' + noShowNum,
            success: function (obj) {
                // 通过eventChannel向被打开页面传送数据
                obj.eventChannel.emit('orderData', {
                    data: res.data,
                    m_addr: res.data.m_addr
                })
            }
        })
    },

    init_val(val, index, arr) {
        val.thumb = this.get_thumb(val.product_img);
        val.price = val.miao_price || val.sweep_price || val.presell_price;
        if (val.seckill_id) {
            val.sku = val.total_num - val.sales;
            if (val.sku < 0) {
                val.sku = 0;
            }
        }

    },
    /**
     *加入购物车事件
     */
    add_cart_click(e) {
        console.log(e);
        this.setData({
            addCart: true,
            pdc: e.detail,
        });
    },
    /**
     * 播放视频
     */
    video_click(e) {
        this.setData({
            showVideo: this.data.imgHost + '/' + e.detail.video
        })
    },

    //切换tab
    onChangeTab(e) {
        console.log(e)
        var urlList = this.data.urlList
        var index = e.detail.index
        this.setData({
            active: index
        })
        if (this.data.title == "斗南专享") {
            if (!urlList[index]) return;

            let wh_id = this.data.wh_id || getApp().globalData.warehouse.wh_id;
            var url = urlList[index] + "?wh_id=" + wh_id + "&is_dounan=1"
            this.setData({
                reqUrl: url,
                current_page: 0,
                pages: 1,
            })
            this.req_items();
        }
        

    },
    subscribeClick_click(e) {
        console.log("预订", e)
        this.setData({
            radioSale: -1,
            showSubscribeClick: true,
            bookPdc: e.detail,
            booknums: 1,
            booksku: 1
        })
    },
    onClose() {
        this.setData({
            showSubscribeClick: false
        })
    },
    //选择预订
    onChangeSale(e) {

        let preArr = this.data.bookPdc.sku_item
        let index = e.detail
        console.log("选择预订", preArr[index].sku)
        this.setData({
            booksku: preArr[index].sku,
            radioSale: index
        })
    },
    booknums_change(e) {
        // this.data.nums = e.detail;
        this.setData({
            booknums: e.detail
        })
        console.log("booknums_change", e)
    },
    //确定
    saleBtn_click(e) {
        console.log("确定", e)
        let state = e.currentTarget.dataset.state;
        if (state != 0) {
            // if(!this.data.checkedSale){
            // 	return wx.showToast({
            // 		title: '请阅读并同意预售交易协议',
            // 		icon: "none"
            // 	})
            // }
            if (this.data.radioSale == -1) {
                return wx.showToast({
                    title: '请选择预售商品',
                    icon: "none"
                })
            }
            console.log("确定radioSale", this.data.radioSale)
            let param = {
                yy_id: this.data.bookPdc.sku_item[this.data.radioSale].yy_id,
                nums: this.data.booknums,
                from: "dounan_yuyue"
            }
            this.request({
                url: this.data.host + '/home/yuyue/preinfo',
                data: param,
                method: 'get'
            }, res => {
                res.data.thumb = this.get_thumb(res.data.product_img, 'wide');
                let that = this;
                wx.navigateTo({
                    url: '/pages/newPreSale/oldsale?wh_id=' + this.data.wh_id +
                        "&from=dounan_yuyue",
                    success: function (obj) {
                        // 通过eventChannel向被打开页面传送数据

                        obj.eventChannel.emit('orderPreData', {
                            wh_id: that.data.wh_id,
                            data: res.data,
                            m_addr: res.m_addr
                        })
                    }
                })

            });

        }
        this.setData({
            showSubscribeClick: false
        })


    },

    onShareAppMessage(res) {
        return {
            title: "",
            path: this.data.path,
            // promise 
        }
    }
})